package com.clss.commandhelper.function.casedetail;

import com.amap.api.location.AMapLocation;
import com.clss.commandhelper.function.newEvidence.InfoTaskDispatch;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import com.clss.commandhelper.CommandHelperApplication;
import com.clss.commandhelper.base.BaseCallback;
import com.clss.commandhelper.base.BaseJson;
import com.clss.commandhelper.base.BasePresenter;
import com.clss.commandhelper.base.BaseView;
import com.clss.commandhelper.tools.janus.entity.MediaSessionBean;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.util.ArrayList;

/**
 *
 * @author liuv
 * @date 2017/11/22
 */

public class CaseDetailPresenter extends BasePresenter {
    private static final String TAG = "CaseDetailPresenter";

    private CaseDetailView mCaseDetailView;
    private CaseDetailModel mCaseDetailModel;

    public void setCaseDetailView(CaseDetailView caseDetailView) {
        this.mCaseDetailView = caseDetailView;
    }

    public void getCaseDetail(){
        mCaseDetailView.showLoadingDialog();
        if (mCaseDetailModel == null){
            mCaseDetailModel = new CaseDetailModel(mCaseDetailView.getActContext());
        }
        mCaseDetailModel.getCaseDetail(mCaseDetailView.getCaseDetailTaskId(),
                new BaseCallback(mCaseDetailView) {
                    @Override
                    public void responseSuccess(BaseJson baseJson) {
                        CaseDetailBean bean = mGson.fromJson(baseJson.getObj(), CaseDetailBean.class);
                        mCaseDetailView.getCaseDetailSuccess(bean);
                        Type typeToken = new TypeToken<List<InfoTaskDispatch>>(){}.getType();
                        List<InfoTaskDispatch> infoTaskDispatches = mGson.fromJson(baseJson.getList().toString(), typeToken);
                        mCaseDetailView.getDispatchSuccess(infoTaskDispatches.get(0));

                        List<DataContentBean> list = new ArrayList<>();
                        JsonElement jsonElement = (JsonElement) baseJson.getList().get(0);
                        JsonObject jsonObject = jsonElement.getAsJsonObject();
                        if(null != jsonObject.get("policeFile")) {
                            JsonArray policeFile = jsonObject.get("policeFile").getAsJsonArray();
                            Type type = new TypeToken<List<DataContentBean>>() {
                            }.getType();
                            list = mGson.fromJson(policeFile, type);
                        }
                        mCaseDetailView.getDetailListSuccess(list);

                    }
                });
    }

    public void changeCaseStatus(int status){
        if (mCaseDetailModel == null){
            mCaseDetailModel = new CaseDetailModel(mCaseDetailView.getActContext());
        }
        mCaseDetailModel.changeCaseStatus(mCaseDetailView.getCaseDetailDispatchId(),
                mCaseDetailView.getCaseDetailTaskId(),
                status,
                mCaseDetailView.getAreaCode(),
                new BaseCallback(mCaseDetailView) {
                    @Override
                    public void responseSuccess(BaseJson baseJson) {
                        mCaseDetailView.changeCaseStatusSuccess(status);
                    }
                });
    }

    public void joinVideoRoom(){
        if (mCaseDetailModel == null){
            mCaseDetailModel = new CaseDetailModel(mCaseDetailView.getActContext());
        }
        double lng = 0, lat = 0;
        String add = "";
        AMapLocation location = CommandHelperApplication.getMapLocation();
        if (null != location && location.getLatitude() > 0.1){
            lng = location.getLongitude();
            lat = location.getLatitude();
            add = location.getAddress();
        }

        mCaseDetailModel.joinVideoRoom(mCaseDetailView.getCaseDetailTaskId(), mCaseDetailView.getCaseDetailDispatchId(),
                lng, lat, add, new BaseCallback(mCaseDetailView) {
                    @Override
                    public void responseSuccess(BaseJson baseJson) {
                        MediaSessionBean bean = mGson.fromJson(baseJson.getObj(), MediaSessionBean.class);
                        mCaseDetailView.joinVideoRoomSuccess(bean);
                    }
                });
    }

    public void refreshTaskLocation(){
        if (mCaseDetailModel == null){
            mCaseDetailModel = new CaseDetailModel(mCaseDetailView.getActContext());
        }
        String add = "", lng = "", lat = "";
        AMapLocation location = CommandHelperApplication.getMapLocation();
        if (null != location && location.getLatitude() > 0.1){
            lng = location.getLongitude() + "";
            lat = location.getLatitude() + "";
            add = location.getAddress();
            mCaseDetailView.showShortToast("正在上传当前位置：\n" + add);
        }else {
            mCaseDetailView.getLocationFail();
            return;
        }
        mCaseDetailModel.refreshLocation(add, lat, lng, mCaseDetailView.getCaseDetailTaskId(),
                new BaseCallback(mCaseDetailView) {
                    @Override
                    public void responseSuccess(BaseJson baseJson) {
                        mCaseDetailView.updateTaskLocationSuccess();
                    }
                });
    }

    @Override
    public void cancelCall() {

    }

    interface CaseDetailView extends BaseView{
        String getCaseDetailTaskId();
        String getCaseDetailDispatchId();
        String getAreaCode();
        void getCaseDetailSuccess(CaseDetailBean bean);
        void joinVideoRoomSuccess(MediaSessionBean bean);
        void changeCaseStatusSuccess(int status);
        void getDetailListSuccess(List<DataContentBean> list);
        void updateTaskLocationSuccess();
        void getLocationFail();
        void getDispatchSuccess(InfoTaskDispatch infoTaskDispatch);

    }
}
